<template>
  <section>
    <!-- 正文区域 -->
    <section class="content">

      <div class="box-body">
        <!--tab页-->
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="商品基本信息" name="first">
            <div class="nav-tabs-custom">
                <!--表单内容-->
              <el-form label-width="80px">
                <el-form-item label="商品分类">
                  <el-cascader style="width: 300px" v-model="categoryId" @change="changeNode"
                      :options="tree"
                      :props="{ checkStrictly: true,
                      label:'label',
                      value:'value'
                       }"
                      ></el-cascader>
                </el-form-item>
                <el-form-item label="商品名称">
                  <el-input v-model="goods.goodsName"></el-input>
                </el-form-item>
                <el-form-item label="品牌">
                  <el-select v-model="goods.brandId" :disabled=false clearable placeholder="请选择">
                    <el-option
                        v-for="item in brands"
                        :key="item.id"
                        :label="item.text"
                        :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="副标题">
                  <el-input v-model="goods.caption"></el-input>
                </el-form-item>
                <el-form-item label="价格">
                  <el-input v-model="goods.price"></el-input>
                </el-form-item>
                <el-form-item label="商品图片">
                  <el-upload
                      class="upload-demo"
                      action="http://localhost:10010/file/upload"
                      v-model="goods.itemImages"
                      :on-preview="handlePreview"
                      :on-remove="handleRemove"
                      :on-success="success"
                      :file-list="fileList"
                      list-type="picture">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                  </el-upload>
                </el-form-item>
                <el-form-item label="商品介绍">
                  <quill-editor v-model="goods.introduction"></quill-editor>
                </el-form-item>
                <el-form-item label="包装列表">
                  <quill-editor v-model="goods.packageList"></quill-editor>
                </el-form-item>
                <el-form-item label="售后服务">
                  <quill-editor v-model="goods.saleService"></quill-editor>
                </el-form-item>
              </el-form>

              <el-button type="primary" @click="save" style="width: 100%">保存</el-button>
            </div>
          </el-tab-pane>
          <el-tab-pane label="扩展属性" name="second">
            <!--扩展属性-->
                <span v-for="cus in customAttributes">
                <el-form ref="form" :model="cus" label-width="80px">
                  <el-form-item :label="cus.text">
                    <el-input v-model="cus.value"></el-input>
                  </el-form-item>
                </el-form>
                  </span>
          </el-tab-pane>
          <el-tab-pane label="规格" name="third">
            <el-table
                :data="specs"
                border
                style="width: 100%">
              <el-table-column
                  prop="text"
                  label="规格名称"
                  width="250">
              </el-table-column>
              <el-table-column
                  label="规格选项"
                  prop="id"
                  width="1134">
                <template slot-scope="scope">
                  <div style="margin-top: 20px">
                    <el-checkbox-group v-model="checkSpec" size="medium">
                      <el-checkbox-button v-for="option in options" :label="option.optionName" v-if="option.specId===scope.row.id"  :key="option.id">
                      </el-checkbox-button>
                    </el-checkbox-group>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </div>
    </section>
  </section>
</template>

<script>
import { quillEditor } from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
/*引入富文本编辑器集成文件上传服务器控件*/
import quillConfig from '@/assets/quill-config.js'

import axios from "axios";

export default {
  components: {
    quillEditor
  },
  data() {
    return {
      option: {
        id:null,
        optionName: '',
        specId:null,
        orders:null
      },
      temp: '',
      options:[],
      quillOption: quillConfig,
      checkSpec: [32],
      specs: [{
        text: '',
        id:null
      }],
      customAttributes: [{
        text: '',
        value: ''
      }],
      brands: [],
      itemId: [],
      fileList: [],
      categoryId: [],
      goods: {
        category1Id:null,
        category2Id:null,
        category3Id:null,
        goodsName: '',
        price: null,
        brandId:null,
        itemImages: '',
        introduction: '',
        packageList: '',
        saleService: ''
      },
      activeName: 'first',
      tree: [{
        label: '',
        value: null
      }]
    }
  },
  methods: {
    save() {
      console.log(this.goods);
      axios.post("/goods/addInfo", this.goods).then(resp => {
        // console.log(resp);
        if (resp.data.code === '1') {
          location.reload();
          this.$message({
            message: '保存成功',
            type: 'success'
          });
        }
      }).catch(resp => {

      });
    },
    changeNode(value) {
          this.goods.category1Id=this.categoryId[0];
          this.goods.category2Id=this.categoryId[1];
          this.goods.category3Id=this.categoryId[2];
      var iterId = value[value.length - 1];
      console.log(iterId);
      var temp = [];
      axios.get("itemcat/queryBrandById/"+iterId).then(resp=>{
        // console.log(resp);
        this.brands = JSON.parse(resp.data.data.brandIds);
        this.customAttributes = JSON.parse(resp.data.data.customAttributeItems);
        this.specs = JSON.parse(resp.data.data.specIds);
        // console.log(this.specs);
        // for (let i = 0; i < this.specs.length; i++) {
        //   temp.push(this.specs[i].id);
        // }
        // console.log(temp);
      }).catch(resp=>{

      });
      // console.log(this.temp);
      axios.get("/spec/getAllOption").then(resp => {
        console.log(resp);
        this.options = resp.data.data;
      }).catch(resp => {

      });
    },
    queryBaran(){
      //查询树结构
      axios.get("/itemcat/queryTree").then(resp => {
        // console.log(resp);
        this.tree = resp.data.data;
      }).catch(resp => {

      });
    },
    success(response,file,fileList) {
      console.log(response);
      console.log(file);
      console.log(fileList);
      this.goods.itemImages=response.data;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleClick(tab, event) {
      console.log(tab, event);
    }
  },
  mounted() {
    this.queryBaran();
  }
}

</script>

<style scoped>

</style>